package com.hwpt.controller.user;

import com.hwpt.dto.user.ResetPasswordDTO;
import com.hwpt.dto.user.UserPwdLoginDTO;
import com.hwpt.dto.user.UserRegisterDTO;
import com.hwpt.dto.user.VerifyCodeDTO;
import com.hwpt.result.Result;
import com.hwpt.service.UserAuthService;
import com.hwpt.service.VerificationCodeService;
import com.hwpt.vo.user.UserAllInfoVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user/auth")
@RequiredArgsConstructor
@Tag(name = "认证接口")
@Slf4j
@Controller("userAuthController")
public class AuthController {

    private final UserAuthService userAuthService;
    private final VerificationCodeService verificationCodeService;

    @PostMapping("/register")
    @ApiOperation("注册")
    public Result<String> register(@RequestBody UserRegisterDTO userRegisterDTO) {
        log.info("注册开始：{}", userRegisterDTO.toString());
        userAuthService.register(userRegisterDTO);
        return Result.success();
    }

    @PostMapping("/login/password")
    @ApiOperation("密码登录")
    public Result<UserAllInfoVO> loginWithPwd(@RequestBody UserPwdLoginDTO userPwdLoginDTO) {
        UserAllInfoVO userAllInfoVO = userAuthService.loginWithPwd(userPwdLoginDTO);
        return Result.success(userAllInfoVO);
    }

    @PostMapping("/login/sms")
    @ApiOperation("短信登录")
    public Result<UserAllInfoVO> loginWithSms(@RequestBody VerifyCodeDTO verifyCodeDTO) {
        verificationCodeService.verifyCode(verifyCodeDTO);
        UserAllInfoVO userAllInfoVO = userAuthService.loginWithSms(verifyCodeDTO);
        log.info("短信登录成功：{}", userAllInfoVO.toString());
        return Result.success(userAllInfoVO);
    }

    @PutMapping("/resetPassword")
    @ApiOperation("重置密码")
    public Result<String> resetPassword(@Valid @RequestBody ResetPasswordDTO resetPasswordDTO) {
        log.info("重置密码开始：{}", resetPasswordDTO.toString());
        userAuthService.resetPassword(resetPasswordDTO);
        return Result.success();
    }


}